<template>
  <div 
    :class="$style.container" 
    class="user">
    <ul>
      <li>
        <div :class="$style.left">
          <span :class="$style.name">头像</span>
          <img v-lazy="avatar1">
        </div>
        <div :class="$style.right">
          <el-upload
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :on-error="handleAvatarError"
            :action="$api.upload"
            :show-file-list="false"
            name="files"
          >
            <el-button 
              round 
            >修改</el-button>
          </el-upload>
        </div>
      </li>
      <li>
        <div :class="$style.left">
          <span :class="$style.name">昵称</span>
          <span :class="$style.f2">{{ $store.state.user.user.name }}</span>
        </div>
        <div :class="$style.right">
          <el-button 
            round 
            @click.stop="handleItemClick('username')">修改</el-button>
        </div>
      </li>
      <li>
        <div :class="$style.left">
          <span :class="$style.name">性别</span>
          <span :class="$style.f2">{{ gender1 }}</span>
        </div>
        <div :class="$style.right">
          <el-button 
            round 
            @click.stop="handleItemClick('gender')">修改</el-button>
        </div>
      </li>
      <li>
        <div :class="$style.left">
          <span :class="$style.name">登录密码</span>
          <span :class="$style.f2">安全强度: 中</span>
        </div>
        <div :class="$style.right">
          <el-button 
            round 
            @click.stop="handleItemClick('password')">修改</el-button>
        </div>
      </li>
    </ul>

    <section>
      <el-dialog 
        :visible.sync="dialogVisible"
        :before-close="handleClose"
        class="alert"
        width="30%" 
        center 
      >
        <div 
          slot="title" 
          :class="$style.alertTitle">
          <span v-if="tab==='username'">修改昵称</span>
          <span v-if="tab==='gender'">修改性别</span>
          <span v-if="tab==='password'">设置密码</span>
        </div>
        <div v-if="tab==='username'">
          <p>
            <span class="name">当前昵称</span>
            <span class="f1">{{ $store.state.user.user.name }}</span>
          </p>
          <p>
            <span class="name">新昵称</span>
            <el-input v-model="username"/>
          </p>
          <div class="tip">以中文或英文字母开头，限4-16个字符。</div>
        </div>
        <div v-if="tab === 'gender'">
          <p>
            <span class="name">性别</span>
            <el-select 
              v-model="gender">
              <el-option 
                :value="1"
                label="男"/>
              <el-option 
                :value="0"
                label="女"/>
            </el-select>
          </p>
        </div>
        <div v-if="tab === 'password'">
          <p>
            <span class="name">当前密码</span>
            <el-input 
              v-model="pwd" 
              type="password"/>
          </p>
          <p>
            <span class="name">密码</span>
            <el-input 
              v-model="password" 
              type="password"/>
          </p>
          <p>
            <span class="name">确认密码</span>
            <el-input 
              v-model="repeatPwd" 
              type="password"/>
          </p>
        </div>
        <span 
          slot="footer" 
          class="dialog-footer">
          <el-button 
            round
            type="primary" 
            @click.stop="handleSubmitClick">确认修改</el-button>
          <el-button 
            round 
            @click.stop="handleCancelClick">取消</el-button>
          
        </span>
      </el-dialog>
    </section>
  </div>
</template>

<script>
import http from '../../APIService/http'
import { signPwd } from '../../util/config'
export default {
  data() {
    return {
      dialogVisible: false,
      tab: '',
      username: '',
      gender: this.$store.state.user.user.gender,
      pwd: '',
      password: '',
      repeatPwd: '',
      avatar: ''
    }
  },
  computed: {
    gender1() {
      return this.$store.state.user.user.gender === 1 ? '男' : '女'
    },
    avatar1() {
      return this.$api.prefix + this.$store.state.user.user.avatar
    }
  },
  mounted() {
    this.gender = this.$store.state.user.user.gender
  },
  methods: {
    // 打开模态框
    handleItemClick(tab) {
      this.tab = tab
      this.dialogVisible = true
    },
    // 关闭模态框
    handleClose() {
      this.dialogVisible = false
    },
    // 上传文件前的检查
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/jpg'
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      return isJPG
    },
    //上传成功
    handleAvatarSuccess(res, file) {
      this.avatar = res.data[0]._id
      this.save()
    },
    //上传失败
    handleAvatarError() {
      this.$message.error('上传头像失败')
    },
    // 重置表单
    reset() {
      this.username = ''
      this.password = ''
      this.repeatPwd = ''
      this.avatar = ''
      this.gender = this.$store.state.user.user.gender
    },
    // 确定按钮
    async handleSubmitClick() {
      this.dialogVisible = false
      this.save()
    },
    // 取消按钮
    handleCancelClick() {
      this.dialogVisible = false
      this.reset()
    },
    async save() {
      try {
        if (this.password !== this.repeatPwd) {
          this.$message({
            type: 'error',
            message: '密码不一致'
          })
          return false
        }
        let params = {
          name: this.username,
          gender: this.gender,
          password: this.password ? signPwd(this.password) : '',
          avatar: this.avatar,
          token: this.$cookiz.get(this.$api.token)
        }
        let res = await http.post(`/users/updateUser`, params)
        if (res.code === 0) {
          let user = this.$store.state.user.user
          if (this.username) {
            user.name = this.username
          }
          user.gender = this.gender
          if (this.avatar) {
            user.avatar = this.avatar
          }
          this.$store.commit('SET_USER', user)
          this.$message({
            type: 'success',
            message: '保存成功'
          })
          this.reset()
        }
      } catch (err) {
        console.log('save userInfo fail:', err)
      }
    }
  }
}
</script>

<style lang='scss' module>
.container {
  ul {
    li {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      border-top: 1px solid #e5e5e5;
      padding: 30px 0;
      .name {
        padding-right: 30px;
        display: inline-block;
        width: 100px;
        height: 40px;
        font-size: 18px;
        color: #333;
        line-height: 34px;
        text-align: right;
        border-right: 1px solid #e5e5e5;
      }
      img {
        margin-left: 30px;
        width: 48px;
        height: 48px;
        border-radius: 48px;
        object-fit: cover;
        vertical-align: middle;
      }
      .f2 {
        margin-left: 30px;
        font-size: 12px;
        color: #666;
        line-height: 40px;
      }
    }
  }
  .alertTitle {
    font-size: 14px;
    font-weight: 500;
    line-height: 50px;
    padding-left: 20px;
    color: #333;
  }
}
</style>

<style scoped>
.user >>> .el-button:focus,
.user >>> .el-button:hover {
  color: #e36049;
  border-color: #e36049;
  background-color: #e360491f;
}
.alert >>> .el-dialog__header {
  text-align: left;
}
.alert >>> .el-button--primary {
  background: #e36049;
  color: #fff;
  border: 1px solid #e36049;
}
.alert >>> .el-button:focus,
.alert >>> .el-button:hover {
  color: #fff;
  border-color: #e36049;
  background-color: #e36049;
}
.alert >>> .el-dialog {
  border-radius: 10px;
}
.alert >>> .el-dialog__header {
  padding: 0px;
  background: #f7f7f7;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}
.alert >>> .el-dialog__headerbtn {
  top: 14px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
}
.alert >>> .el-dialog__close {
  color: #fff;
  font-weight: 700;
}
.alert >>> .el-input {
  width: calc(100% - 100px);
}
.alert .name {
  margin-right: 10px;
  display: inline-block;
  width: 80px;
  text-align: right;
}
.alert .f1 {
  font-size: 14px;
  color: #666;
  line-height: 40px;
  font-weight: 500;
}
.alert p {
  margin-bottom: 5px;
}
.alert .tip {
  font-size: 12px;
  color: #999;
  line-height: 1.2;
  margin: 10px 0 10px 100px;
  line-height: 20px;
}
.alert >>> .el-input__inner:focus {
  border-color: #e36049;
}
.alert >>> .el-input--suffix {
  width: 200px;
}
.alert >>> .el-input__inner {
  width: 200px;
}
</style>
<style>
.selected {
  color: #e36049 !important;
}
</style>
